/*
*  Engine.h
*  SypXIPhone
*
*  Created by Luv Khemani on 5/14/10.
*  Copyright 2010 Luv Khemani. All rights reserved.
*
*/

#pragma once
#ifndef SYPX_ENGINE
#define SYPX_ENGINE
#include "Canvas.h"
#include "Singleton.h"
#include "Renderer.h"
#include "AudioEngine.h"
#include "Font.h"

//FreeType2 defines since we dont want to include freetype.h in headers
struct FT_LibraryRec_;
typedef struct FT_LibraryRec_* FT_Library;

namespace SypX
{
	///The Engine provides factory methods which creates objects based on the platform
	///and hides all platform specific code from the user.
	class SYPXAPI Engine : public Singleton<Engine>
	{
	public:	
		///Initialises Engine and all subsystems in the correct order
		///Width and Height values are ignored on the iPhone(SypX only supports fullscreen iPhone apps atm)
		///Starts running the engine. Control is not returned until application is closed.
		void initAndStart(UInt width = 0, UInt height = 0);
		///Width and Height values are ignored on the iPhone(SypX only supports fullscreen iPhone apps atm)
		Canvas* createCanvas(UInt width = 0, UInt height = 0);
		///Creates and returns a pointer to a Renderer
		Renderer* createRenderer();		
		///Returns the path to a file in a bundle on IPhone. Returns the argument string unchanged on other platforms
		///On iPhone : any folders leading to the file are stripped and the filename is correctly split into filename and extension 
		///to retrieve the full path of the file on the iPhone. Therefore it is safe to pass in files not in the current directory
		static String getFilePath(const String& filename);
		///Registers a run loop callback function (Optional for use on most desktop platforms, but needed for platforms which takeover mainloop)
		///You should register a runloop callback if you intend to port your application on such platforms
		virtual void registerRunLoop(void (*runLoopCallBack)(void));
		///Registers an Init callback function
		///Init is called once after the Canvas is initialised
		virtual void registerInitFunction(void (*initCallBack)(void));			
		///Returns a pointer to the Renderer
		Renderer* getRenderer();
		///Returns a pointer to the Canvas
		Canvas* getCanvas();
		///Returns a pointer to the AudioEngine
		AudioEngine* getAudioEngine();
		void (*runFunction)();
		void (*initFunction)();
		///Inits subsystems in correct order before calling userland init. Should not be called by User
		void init(UInt width = 0, UInt height = 0);
		///Loads and returns a font from a font file.
		///Supports all font formats supported by FreeType2. NOTE: PCF, gzip compressed fonts not supported on iPhone
		///Font can only be removed/deleted via Engine::removeFont().
		///genColor indicates whether this font should support per character color coding(more memory&bandwidth usage). Disabled by default
		Font* loadFontFromFile(const String& name, int size, int MARGIN = 2, bool genColor = false);
		/*!Removes/deletes the Font. All Fonts should be removed this way.
		Note: This results in an absolute removal of the Font from memory. Reloading the same font will require a full reload
		and regeneration of internal font textures and you should therefore only delete fonts if you need the memory or will not need it soon.*/
		void removeFont(Font* f);
	private:		

		Engine();
		~Engine();
		///Creates and returns a pointer to AudioEngine
		AudioEngine* createAudioEngine();
		friend class Singleton<Engine>;
		Canvas* canvas;
		Renderer* render;
		AudioEngine* audioEng;
		FT_Library  ft2Lib;	//Freetype2 font library


	};

}
#endif